package com.liveneo.adapter.mobile.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liveneo.adapter.base.entity.PrpmUserOnline;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.BaseController;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.BaseRequest;
import com.liveneo.adapter.mobile.model.BaseResponse;
import com.liveneo.adapter.mobile.model.request.CallbackRequest;
import com.liveneo.adapter.mobile.model.response.OcrImageResponse;
import com.liveneo.adapter.mobile.service.PrpmUserOnlineService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.system.base.utils.JsonUtils;

/**
 * @Desc：用户状态接口
 * @Title：MobileUserOnlineController
 * @address: http://www.liveneo.com.cn/
 * @author: zhaoyy
 * @version: 1.0 
 * @date: 2016年11月23日 下午1:56:38
 */
@Controller
@RequestMapping("/mobile")
public class MobileUserOnlineController extends BaseController {
    @Autowired
    private PrpmUserOnlineService userOnlineService;

    /**
     * MP19
     * @Desc: 更新客户端在线时间
     * @Author: zhaoyy 2016年11月23日 下午1:59:19
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping("/mobileuserOnline.action")
    public String mobileuserOnline(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP19入参=" + loguuid;
        BaseRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, BaseRequest.class); // 解析请求参数
        BaseResponse response = null;
        String result = "";
        String userCode = request.getUserId();
        String userType = request.getProjectCode();
        int sign = 0;
        try {
            if (StringUtils.isBlank(userCode) || StringUtils.isBlank(userType)) {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_NOVALID_PARAM, MobileReturnCodeSupport.SYS_CODE,
                        MobileReturnCodeSupport.CODE_PARAM_NO_NOT, OcrImageResponse.class);
            } else {
                PrpmUserOnline userOnline = userOnlineService.selectByUserCodeAndUserType(userCode, userType);
                if (userOnline != null) {
                    sign = userOnlineService.updateUserOnline(userOnline, request); // 更新
                } else {
                    sign = userOnlineService.saveUserOnline(request); // 保存
                }
                if (sign == 1) {
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                            BaseResponse.class);
                } else {
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE, "更新在线时间失败",
                            BaseResponse.class);
                }
            }
        } catch (Exception e) {
            LogUtils.error("MP19更新客户端在线时间==>错误信息:", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    OcrImageResponse.class);
        } finally {
            result = decorateResult("MP19更新客户端在线时间", response);
        }
        return result;
    }

    /**
     * MP71
     * @Desc: 移动端视频组建在线告知接口
     * @Author: zhaoyy 2016年11月30日 上午10:12:08
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping("/callback.action")
    public String callback(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP71入参=" + loguuid;
        CallbackRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, CallbackRequest.class); // 解析请求参数
        BaseResponse response = null;
        String result = "";
        String userId = request.getUserId();        // 用户id
        String projectCode = request.getProjectCode();
        try {
            if (StringUtils.isBlank(userId) || StringUtils.isBlank(projectCode)) {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_NOVALID_PARAM, MobileReturnCodeSupport.SYS_CODE,
                        MobileReturnCodeSupport.CODE_PARAM_NO_NOT, OcrImageResponse.class);
            } else {
                String strredis = userOnlineService.savaCallbackRedis(request);
                if (StringUtils.isNotBlank(strredis)) {
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                            BaseResponse.class);
                } else {
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE, "更新在线时间失败",
                            BaseResponse.class);
                }
            }
        } catch (Exception e) {
            LogUtils.error("MP71移动端视频组建在线告知接口==>错误信息:", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    OcrImageResponse.class);
        } finally {
            result = decorateResult("MP71出参=>" + loguuid, response);
        }
        return result;
    }
}
